﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameVer1
{
    public class NewUnit
    {
        #region Attribute

        private int _X;
        public int X
        {
            get { return _X; }
            set { _X = value; }
        }
        private int _Y;
        public int Y
        {
            get { return _Y; }
            set { _Y = value; }
        }
        private Texture2D _UnitTexture;
        public Texture2D UnitTexture
        {
            get { return _UnitTexture; }
            set { _UnitTexture = value; }
        }
        private Vector2 _direction;
        public Vector2 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        private int _timeSinceLastFrame;
        public int TimeSinceLastFrame
        {
            get { return _timeSinceLastFrame; }
            set { _timeSinceLastFrame = value; }
        }
        private int _timePerFrame;
        public int TimePerFrame
        {
            get { return _timePerFrame; }
            set { _timePerFrame = value; }
        }
        private Vector2 _currentFrame;
        public Vector2 CurrentFrame
        {
            get { return _currentFrame; }
            set { _currentFrame = value; }
        }
        private Vector2 _numberFrame;
        public Vector2 NumberFrame
        {
            get { return _numberFrame; }
            set { _numberFrame = value; }
        }
        private Point _SizeFrame;
        public Point SizeFrame
        {
            get { return _SizeFrame; }
            set { _SizeFrame = value; }
        }
        private Vector2 _speed;
        public Vector2 Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }
        private Color _filterColor;
        public Color FilterColor
        {
            get { return _filterColor; }
            set { _filterColor = value; }
        }
        private Rectangle _coverRect;
        public Rectangle CoverRect
        {
            get { return _coverRect; }
            set { _coverRect = value; }
        }
        private float scale;
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        private int _currentPos;
        public int CurrentPos
        {
            get { return _currentPos; }
            set { _currentPos = value; }
        }
        private int _nextPos;
        public int NextPos
        {
            get { return _nextPos; }
            set { _nextPos = value; }
        }
        private int lastAnimation;
        public int LastAnimation
        {
            get { return lastAnimation; }
            set { lastAnimation = value; }
        }
        public int[,] Path;
        private int _flagDraw;
        public int FlagDraw
        {
            get { return _flagDraw; }
            set { _flagDraw = value; }
        }
        private int _updateFlag;
        public int UpdateFlag
        {
            get { return _updateFlag; }
            set { _updateFlag = value; }
        }
        #endregion

        #region Constructor

        public NewUnit() //Not use
        {

        }
        //In use
        public NewUnit(Texture2D texture,Vector2 position, int timesincrelastframe, int timeperframe,int currentPath, Vector2 speed, float scale,Point sizeFrame,Vector2 currentFrame)
        {
            InitializePath();
            this.UnitTexture = texture;
            this.TimeSinceLastFrame = timesincrelastframe;
            this.TimePerFrame = timeperframe;
            this.Scale = scale;
            this.CurrentPos = currentPath;
            this.Direction = position;
            this.Speed = speed;
            this.CurrentFrame = currentFrame;
            this.SizeFrame = sizeFrame; //co dinh
            this.NumberFrame = new Vector2(8, 8); //co dinh
            this.NextPos = this.CurrentPos+ 48;
            this.LastAnimation = -1;
            this.FlagDraw = 1;
            this.UpdateFlag = 0;
            this.CoverRect = new Rectangle((int)this.Direction.X + this.SizeFrame.X - 5, (int)this.Direction.Y + this.SizeFrame.Y - 5, 5, 5);
        }
        
        #endregion

        #region Menthod

        public void Draw(SpriteBatch spriteBatch)
        {
            if (FlagDraw == 1)
            {
                spriteBatch.Draw(UnitTexture, Direction, new Rectangle((int)CurrentFrame.X * SizeFrame.X, (int)CurrentFrame.Y * SizeFrame.Y, SizeFrame.X, SizeFrame.Y), Color.White, 0, Vector2.Zero, this.Scale, SpriteEffects.None,(float)0.5);
            }
        }
        public void Update(Game1 game1, GameTime gameTime)
        {
            if (FlagDraw == 1&& UpdateFlag ==1)
            {
                float tempX, tempY;
                tempX = tempY = 0;
                TimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (TimeSinceLastFrame > TimePerFrame)
                {
                    this.CurrentFrame = new Vector2(Path[CurrentPos % 48, 2], Path[CurrentPos % 48, 3]);
                    TimeSinceLastFrame -= TimePerFrame;
                    if (CurrentPos < NextPos)
                    {
                        int flag1, flag2;
                        flag1 = flag2 = 0;
                        lastAnimation += 1;
                        this.CurrentFrame = new Vector2(lastAnimation % 8, this.CurrentFrame.Y % 8);
                        if (Path[CurrentPos % 48, 0] <= Path[(CurrentPos + 1) % 48, 0])
                        {
                            if (this.Direction.X <= Path[(CurrentPos + 1) % 48, 0])
                            {
                                tempX = this.Direction.X + 4;
                            }
                            else
                            {
                                flag1 = 1;
                                tempX = this.Direction.X;
                            }
                        }
                        else if (Path[CurrentPos % 48, 0] > Path[(CurrentPos + 1) % 48, 0])
                        {
                            if (this.Direction.X >= Path[(CurrentPos + 1) % 48, 0])
                            {
                                tempX = this.Direction.X - 4;
                            }
                            else
                            {
                                flag1 = 1;
                                tempX = this.Direction.X;
                            }
                        }
                        if (Path[CurrentPos % 48, 1] <= Path[(CurrentPos + 1) % 48, 1])
                        {
                            if (this.Direction.Y <= Path[(CurrentPos + 1) % 48, 1])
                            {
                                tempY = this.Direction.Y + 4;
                            }
                            else
                            {
                                flag2 = 1;
                                tempY = this.Direction.Y;
                            }
                        }
                        else if (Path[CurrentPos % 48, 1] > Path[(CurrentPos + 1) % 48, 1])
                        {
                            if (this.Direction.Y >= Path[(CurrentPos + 1) % 48, 1])
                            {
                                tempY = this.Direction.Y - 4;
                            }
                            else
                            {
                                flag2 = 1;
                                tempY = this.Direction.Y;
                            }
                        }
                        if (flag1 == 1 && flag2 == 1)
                        {
                            CurrentPos++;
                        }
                        this.Direction = new Vector2((int)tempX, (int)tempY);
                        UpdateCoverFootRect();
                    }
                }
            }
        }
        public void UpdateCoverFootRect()
        {
            this.CoverRect = new Rectangle((int)this.Direction.X + this.SizeFrame.X - 10, (int)this.Direction.Y + this.SizeFrame.Y - 10, 10, 10);
        }
        public void InitializePath()
        {
            Path = new int[48, 4];
            Path[0, 0] = 210; Path[0, 1] = 110; Path[0, 2] = 0; Path[0, 3] = 0;
            Path[1, 0] = 220; Path[1, 1] = 132; Path[1, 2] = 0; Path[1, 3] = 0;
            Path[2, 0] = 230; Path[2, 1] = 147; Path[2, 2] = 0; Path[2, 3] = 0;
            Path[3, 0] = 237; Path[3, 1] = 165; Path[3, 2] = 0; Path[3, 3] = 0;
            Path[4, 0] = 243; Path[4, 1] = 190; Path[4, 2] = 0; Path[4, 3] = 4;
            Path[5, 0] = 240; Path[5, 1] = 215; Path[5, 2] = 0; Path[5, 3] = 4;
            Path[6, 0] = 227; Path[6, 1] = 235; Path[6, 2] = 0; Path[6, 3] = 4;
            Path[7, 0] = 205; Path[7, 1] = 253; Path[7, 2] = 0; Path[7, 3] = 4;
            Path[8, 0] = 181; Path[8, 1] = 278; Path[8, 2] = 0; Path[8, 3] = 4;
            Path[9, 0] = 163; Path[9, 1] = 295; Path[9, 2] = 0; Path[9, 3] = 4;
            Path[10, 0] = 145; Path[10, 1] = 310; Path[10, 2] = 0; Path[10, 3] = 4;
            Path[11, 0] = 147; Path[11, 1] = 340; Path[11, 2] = 0; Path[11, 3] = 0;
            Path[12, 0] = 177; Path[12, 1] = 360; Path[12, 2] = 0; Path[12, 3] = 7;
            Path[13, 0] = 210; Path[13, 1] = 360; Path[13, 2] = 0; Path[13, 3] = 7;
            Path[14, 0] = 250; Path[14, 1] = 358; Path[14, 2] = 0; Path[14, 3] = 7;
            Path[15, 0] = 275; Path[15, 1] = 345; Path[15, 2] = 0; Path[15, 3] = 2;
            Path[16, 0] = 315; Path[16, 1] = 340; Path[16, 2] = 0; Path[16, 3] = 2;
            Path[17, 0] = 345; Path[17, 1] = 340; Path[17, 2] = 0; Path[17, 3] = 2;
            Path[18, 0] = 375; Path[18, 1] = 345; Path[18, 2] = 0; Path[18, 3] = 2;
            Path[19, 0] = 395; Path[19, 1] = 365; Path[19, 2] = 0; Path[19, 3] = 5;
            Path[20, 0] = 425; Path[20, 1] = 392; Path[20, 2] = 0; Path[20, 3] = 5;
            Path[21, 0] = 450; Path[21, 1] = 410; Path[21, 2] = 0; Path[21, 3] = 5;
            Path[22, 0] = 475; Path[22, 1] = 420; Path[22, 2] = 0; Path[22, 3] = 5;
            Path[23, 0] = 525; Path[23, 1] = 410; Path[23, 2] = 0; Path[23, 3] = 2;
            Path[24, 0] = 550; Path[24, 1] = 370; Path[24, 2] = 0; Path[24, 3] = 3;
            Path[25, 0] = 540; Path[25, 1] = 350; Path[25, 2] = 0; Path[25, 3] = 3;
            Path[26, 0] = 520; Path[26, 1] = 320; Path[26, 2] = 0; Path[26, 3] = 3;
            Path[27, 0] = 510; Path[27, 1] = 300; Path[27, 2] = 0; Path[27, 3] = 3;
            Path[28, 0] = 490; Path[28, 1] = 275; Path[28, 2] = 0; Path[28, 3] = 3;
            Path[29, 0] = 490; Path[29, 1] = 250; Path[29, 2] = 0; Path[29, 3] = 7;
            Path[30, 0] = 510; Path[30, 1] = 230; Path[30, 2] = 0; Path[30, 3] = 7;
            Path[31, 0] = 525; Path[31, 1] = 215; Path[31, 2] = 0; Path[31, 3] = 7;
            Path[32, 0] = 545; Path[32, 1] = 195; Path[32, 2] = 0; Path[32, 3] = 7;
            Path[33, 0] = 560; Path[33, 1] = 175; Path[33, 2] = 0; Path[33, 3] = 7;
            Path[34, 0] = 580; Path[34, 1] = 155; Path[34, 2] = 0; Path[34, 3] = 7;
            Path[35, 0] = 570; Path[35, 1] = 130; Path[35, 2] = 0; Path[35, 3] = 6;
            Path[36, 0] = 540; Path[36, 1] = 120; Path[36, 2] = 0; Path[36, 3] = 1;
            Path[37, 0] = 505; Path[37, 1] = 125; Path[37, 2] = 0; Path[37, 3] = 1;
            Path[38, 0] = 475; Path[38, 1] = 130; Path[38, 2] = 0; Path[38, 3] = 1;
            Path[39, 0] = 445; Path[39, 1] = 135; Path[39, 2] = 0; Path[39, 3] = 1;
            Path[40, 0] = 420; Path[40, 1] = 135; Path[40, 2] = 0; Path[40, 3] = 1;
            Path[41, 0] = 390; Path[41, 1] = 130; Path[41, 2] = 0; Path[41, 3] = 1;
            Path[42, 0] = 370; Path[42, 1] = 120; Path[42, 2] = 0; Path[42, 3] = 6;
            Path[43, 0] = 340; Path[43, 1] = 110; Path[43, 2] = 0; Path[43, 3] = 6;
            Path[44, 0] = 320; Path[44, 1] = 100; Path[44, 2] = 0; Path[44, 3] = 6;
            Path[45, 0] = 290; Path[45, 1] = 90; Path[45, 2] = 0; Path[45, 3] = 6;
            Path[46, 0] = 270; Path[46, 1] = 80; Path[46, 2] = 0; Path[46, 3] = 1;
            Path[47, 0] = 230; Path[47, 1] = 90; Path[47, 2] = 0; Path[47, 3] = 1;
        }
        #endregion
    }
}
